home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / readers / utils / gui4cli / demos / calc.gc next >
Text File  |  1996-10-28  |  11KB  |  425 lines

  1. G4C
  2.  
  3. ; CALC.gc 
  4.  
  5. ; A calculator, based on the EVAL cli command, which should
  6. ; be in your c: directory.
  7.  
  8. ; What we do here, is make a gui to construct the calculation formula
  9. ; that we will pass to EVAL, for us to solve.
  10.  
  11. ; EVAL will read in the formula and place the result in an env: Variable
  12. ; we tell it. From there, we read it and redisplay it to the user.
  13.  
  14. ; Not satisfied with our above incredible feat, we go on to add 5 memories,
  15. ; and Octal and Hex conversion. (somebody stop me!)
  16.  
  17. ; The only fault in this program, is that EVAL will not support floating
  18. ; point numbers, so we can't have decimals. If someone were to write
  19. ; a little program that did support decimals, and preferably AREXX too, 
  20. ; then this could be a calculator everyone could adjust to his tastes.
  21.  
  22. ; PS : I wrote the above before seeing GUIs:G4C/Various/FPCalc.gc,
  23. ; by G.Maddox, where he uses ARexx's calculating capabilities to
  24. ; make an excellent floating point calc.. oh well.. I'll change it next
  25. ; time..
  26.  
  27.  
  28. WINBIG   400 24 223 155 "EVAL"   ; Our window.
  29. WinType  11110001                ; It has all standard gadgets & bottom resize
  30. WinSmall 0 -1 181 122            ; The minimum size of our window
  31. WINOUT   "NIL:"                  ; We don't want any output.
  32. WINBACKGROUND PATTERN 0 2        ; Add a flashy background..
  33. ;NoFontSense
  34.  
  35. ;***************************** GRAPHICS **************************************
  36.  
  37. line 10 90 213 90 1       ; Some lines, for unparalleled dramatic effect.
  38. line 10 91 213 91 2
  39. line 85 112 213 112 1
  40. line 86 113 213 113 2
  41. line 85 112 85  129 1
  42. line 86 113 86  130 2
  43. line 10 129 85  129 1
  44. line 10 130 86  130 2
  45.  
  46. BOX  0 0 0 0 OUT ICONDROP ; A window sized box, taking us way beyond "cool"
  47.  
  48. ;************************** GENERAL EVENTS ***********************************
  49.  
  50. xAPPICON 0 0 guis:info/calc "Calculator" "" OFF      ; An AppIcon which when
  51. GadID 10                                             ; clicked on will
  52. GuiOpen calc.gc                                      ; open the calculator.
  53.  
  54. xONOPEN                      ; Every time the window opens, 
  55. SetGad calc.gc 10 OFF        ; we remove the above AppIcon
  56. GuiScreen calc.gc FRONT      ; and bring the screen to the front.
  57.  
  58. xONCLOSE                     ; Every time the user closes the window, 
  59. SetGad calc.gc 10 ON         ; we make the AppIcon re-appear.
  60.  
  61. xONLOAD                     ; Upon loading of the GUI
  62. setvar calcvar ""           ; This is our main variable
  63. GUIOPEN calc.gc
  64. RUN 'resident c:eval ADD'   ; Make eval resident, for speed.
  65. setvar lformat "%N"         ; This is the display format we want
  66. setvar mem  .mem1           ; These are the Memory variables
  67. setvar .mem1 0              ;     The ones starting with a full stop,
  68. setvar .mem2 0              ;     are env: variables
  69. setvar .mem3 0
  70. setvar .mem4 0
  71. setvar .mem5 0
  72. setvar membuff ""
  73. setvar .result "0"          ; somewhere to put the result (env: variable)
  74.  
  75. xOnQuit                     ; On quitting we delete the env variables, 
  76. delete env:.mem#?           ; so they don't linger in Env:
  77. delvar calcvar
  78. delvar lformat
  79. delvar mem
  80. delvar membuff
  81. delvar .result
  82. RUN "Resident eval remove"  ; Remove eval from the resident list
  83.  
  84. ;********************************* MENUS *************************************
  85. ; Below are the Menus for our calculator. They are pretty basic.
  86.  
  87.  
  88. xMENU Calculator About... "" A
  89. EZREQ "Calculator made with Gui4Cli\nUsing the c:Eval command" "Incredible !!" CalcReq
  90.  
  91. xMENU Calculator BARLABEL "" ""
  92.  
  93. xMENU calculator Quit "" Q
  94. GUIQUIT calc.gc
  95.  
  96. xMENU Options Screen Workbench ""   ; This will place the calc on the WB screen
  97. SetScreen calc.gc Workbench         ; Set screen to workbench
  98. gosub calc.gc ScreenChange          ; See routine "ScreenChange"
  99.  
  100. xMENU Options Screen CygnusEd ""    ; Place it on CygnusEd screen
  101. SetScreen calc.gc CygnusEdScreen1    
  102. gosub calc.gc ScreenChange
  103.  
  104. xMENU Options Screen GoldEd ""      ; GoldEd screen
  105. SetScreen calc.gc GOLDED.1    
  106. gosub calc.gc ScreenChange
  107.  
  108. xROUTINE ScreenChange               ; Routine to change screen
  109. GuiClose calc.gc                    ; Close & reopen window, so that change
  110. GuiOpen  calc.gc                    ; takes effect
  111. GuiScreen calc.gc FRONT             ; bring window's screen to front
  112.  
  113. xMENU Options Display Decimal D     ; Change Decimal/Octal/Hex
  114. update calc.gc 2 0
  115. gosub calc.gc FormatChange
  116.  
  117. xMENU options display Octal C
  118. update calc.gc 2 2
  119. gosub calc.gc FormatChange
  120.  
  121. xMENU options display Hex H
  122. update calc.gc 2 1
  123. gosub calc.gc FormatChange
  124.  
  125. xMENU options BARLABEL "" ""
  126.  
  127. xMENU options Calculate "" ""
  128. gosub calc.gc calculate
  129.  
  130. xMENU options Clear "" C
  131. update calc.gc  1 0
  132. setvar calcvar ""
  133.  
  134.  
  135. ;*****************************************************************************
  136. ;***************************** BUTTONS GALORE ! ******************************
  137. ; All these buttons do the same thing. They AppVar their number or letter
  138. ; to our main variable and redisplay it
  139.  
  140.  
  141. ;=======================> The Buttons for the plain numbers
  142.  
  143. xBUTTON 10 25 25 15 1
  144. gadkey 1                     ; Keyboard shortcut
  145. AppVar calcvar 1             ; AppVar the number to "calcvar"
  146. update calc.gc  1 $calcvar   ; and re-display it.
  147.  
  148. xBUTTON 36 25 25 15 2        ; same as above
  149. gadkey  2
  150. AppVar calcvar 2
  151. update calc.gc  1 $calcvar
  152.  
  153. xBUTTON 62 25 25 15 3
  154. gadkey  3
  155. AppVar calcvar 3
  156. update calc.gc  1 $calcvar
  157.  
  158. xBUTTON 10 41 25 15 4
  159. gadkey  4
  160. AppVar calcvar 4
  161. update calc.gc  1 $calcvar
  162.  
  163. xBUTTON 36 41 25 15 5
  164. gadkey  5
  165. AppVar calcvar 5
  166. update calc.gc  1 $calcvar
  167.  
  168. xBUTTON 62 41 25 15 6
  169. gadkey  6
  170. AppVar calcvar 6
  171. update calc.gc  1 $calcvar
  172.  
  173. xBUTTON 10 57 25 15 7
  174. gadkey  7
  175. AppVar calcvar 7
  176. update calc.gc  1 $calcvar
  177.  
  178. xBUTTON 36 57 25 15 8
  179. gadkey  8
  180. AppVar calcvar 8
  181. update calc.gc  1 $calcvar
  182.  
  183. xBUTTON 62 57 25 15 9
  184. gadkey  9
  185. AppVar calcvar 9
  186. update calc.gc  1 $calcvar
  187.  
  188. xBUTTON 10 73 25 15 0
  189. gadkey  0
  190. AppVar calcvar 0
  191. update calc.gc  1 $calcvar
  192.  
  193. ;=========================> The operators
  194.  
  195. xBUTTON 90 25 25 15 /
  196. gadkey  /
  197. AppVar calcvar /
  198. update calc.gc  1 $calcvar
  199.  
  200. xBUTTON 90 41 25 15 *
  201. gadkey  *
  202. AppVar calcvar *
  203. update calc.gc  1 $calcvar
  204.  
  205. xBUTTON 90 57 25 15 -
  206. gadkey  -
  207. AppVar calcvar -
  208. update calc.gc  1 $calcvar
  209.  
  210. xBUTTON 90 73 25 15 +
  211. gadkey  +
  212. AppVar calcvar +
  213. update calc.gc  1 $calcvar
  214.  
  215. xBUTTON 116 25 25 15 &
  216. gadkey  &
  217. AppVar calcvar &
  218. update calc.gc  1 $calcvar
  219.  
  220. xBUTTON 116 41 25 15 |
  221. gadkey  |
  222. AppVar calcvar |
  223. update calc.gc  1 $calcvar
  224.  
  225. xBUTTON 116 57 25 15 (
  226. gadkey  (
  227. AppVar calcvar (
  228. update calc.gc  1 $calcvar
  229.  
  230. xBUTTON 116 73 25 15 )
  231. gadkey  )
  232. AppVar calcvar )
  233. update calc.gc  1 $calcvar
  234.  
  235. xBUTTON 142 25 35 15 >>
  236. gadkey  >>
  237. AppVar calcvar >>
  238. update calc.gc  1 $calcvar
  239.  
  240. xBUTTON 142 41 35 15 <<
  241. gadkey  <<
  242. AppVar calcvar <<
  243. update calc.gc  1 $calcvar
  244.  
  245. xBUTTON 142 57 35 15 _mod
  246. AppVar calcvar mod
  247. update calc.gc  1 $calcvar
  248.  
  249. xBUTTON 142 73 35 15 ~
  250. gadkey ~ 
  251. AppVar calcvar ~
  252. update calc.gc  1 $calcvar
  253.  
  254. xBUTTON 178 25 35 15 x_or
  255. AppVar calcvar xor
  256. update calc.gc  1 $calcvar
  257.  
  258. xBUTTON 178 41 35 15 _eqv
  259. AppVar calcvar eqv
  260. update calc.gc  1 $calcvar
  261.  
  262. xBUTTON 178 57 35 15 BSP       ; This is a BackSpace button
  263. gadkey #8
  264. cutvar calcvar CUT CHAR -1 ""   ; It will delete the last character of
  265. update calc.gc  1 $calcvar     ; "calcvar" and re-display it
  266.  
  267. xBUTTON 178 73 35 15 CLR
  268. gadkey #127
  269. update calc.gc  1 0            ; Clear button
  270. setvar calcvar ""
  271.  
  272. ;==========================> Calculate the result and display it.
  273.  
  274. xBUTTON 36 73 51 15 =
  275. gadkey #13
  276. gosub calc.gc calculate        ; GoSub the routine which does the calculation
  277.  
  278. ;===========================> This is our display panel. - Note it's
  279. ; a xTEXTin type gadget, so we can enter a calculation directly into it.
  280.  
  281. xTEXTIN 10 5 203 15 "" calcvar "Hello There!" 160
  282. GADID 1
  283.  
  284. ;===========================> Buttons for HEX input
  285.  
  286. xBUTTON 10 95 25 15 A
  287. gadkey  A
  288. AppVar calcvar A
  289. update calc.gc  1 $calcvar
  290.  
  291. xBUTTON 36 95 25 15 B
  292. gadkey  B
  293. AppVar calcvar B
  294. update calc.gc  1 $calcvar
  295.  
  296. xBUTTON 62 95 25 15 C
  297. gadkey  C
  298. AppVar calcvar C
  299. update calc.gc  1 $calcvar
  300.  
  301. xBUTTON 88 95 25 15 D
  302. gadkey  D
  303. AppVar calcvar D
  304. update calc.gc  1 $calcvar
  305.  
  306. xBUTTON 114 95 25 15 E
  307. gadkey  E
  308. AppVar calcvar E
  309. update calc.gc  1 $calcvar
  310.  
  311. xBUTTON 140 95 25 15 F
  312. gadkey  F
  313. AppVar calcvar F
  314. update calc.gc  1 $calcvar
  315.  
  316. ; These two are for octal (#Number) and hex (#xNumber) input
  317.  
  318. xBUTTON 170 95 20 15 #
  319. AppVar calcvar #
  320. update calc.gc  1 $calcvar
  321.  
  322. xBUTTON 191 95 20 15 x
  323. gadkey  x
  324. AppVar calcvar x
  325. update calc.gc  1 $calcvar
  326.  
  327.  
  328. ;==========================> and a cycler to choose which format we want
  329. ; NOTE : Eval needs a number in the hex and octal display format, stating
  330. ;        how many digits it should display. The maximum is 9 (I think).
  331.  
  332.  
  333. xCYCLER 10 112 70 15 "" lformat
  334. GADID 2
  335. CSTR DEC %N      ; These are the arguments that EVAL needs to give
  336. CSTR HEX #x%X9   ; us the required display.
  337. CSTR OCT #%O9
  338. gadkey H
  339. gosub calc.gc FormatChange
  340.  
  341.  
  342. ;****************************** ROUTINES *************************************
  343.  
  344. ;===================> routine to calculate & display result
  345.  
  346. xROUTINE calculate
  347. if $calcvar > ""                                         ; If there is an entry
  348.    CLI 'eval $calcvar TO env:.result LFORMAT= $lformat'   ; send it to EVAL
  349.    update calc.gc  1  $.result      ; and display the answer EVAL returned
  350.    setvar calcvar $.result          ; We set "calcvar" to the result
  351.    if $calcvar = 0 
  352.       setvar calcvar ""            ; and if it's 0, we empty it (looks nicer)
  353.    endif
  354. endif 
  355.  
  356. ;==================> routine to show result in DEC/OCT/HEX format
  357. ;                    Basically the same routine as above.
  358.  
  359. xROUTINE FormatChange
  360. if $calcvar = ""
  361.    setvar calcvar 0
  362. endif
  363. CLI 'eval $calcvar TO env:.result LFORMAT= $lformat'
  364. update calc.gc  1  $.result
  365. setvar calcvar $.result
  366. if $calcvar = 0 
  367.    setvar calcvar ""
  368. endif
  369.  
  370.  
  371. ;*****************************************************************************
  372. ;***************************** MEMORIES **************************************
  373.  
  374. ; If you look at the following code carefully, you will see that we
  375. ; need a variable within a variable. 
  376. ; We use "membuff" to construct such a variable.
  377.  
  378. ; This may be a little hard to understand, but it's nothing you'll need.
  379. ; If you don't understand it, it's not important.
  380.  
  381.  
  382. ;=======================> Memories display panel
  383.  
  384. xTEXTIN 91 116 123 15 "" $mem 0 30
  385. GadID 5
  386.  
  387. ;=======================> Memories cycler
  388.  
  389. xCycler 10 134 70 15 "" mem
  390. Cstr "M1" .mem1
  391. Cstr "M2" .mem2
  392. Cstr "M3" .mem3
  393. Cstr "M4" .mem4
  394. Cstr "M5" .mem5
  395. setvar membuff "\$$mem"    ; This means set membuff to $ + whatever is in "mem"
  396. update calc.gc 5 $membuff  ; so that it points to the correct variable
  397.  
  398. ;=======================> Memory IN/OUT
  399.  
  400. xButton 80 134 33 15 IN        ; These buttons do the usual MEM in/out/+/-
  401. SetVar $mem $calcvar           ; stuff that calculators do.
  402. setvar membuff "\$$mem"
  403. update calc.gc 5 $membuff
  404.  
  405. xButton 113 134 33 15 OUT
  406. setvar membuff "\$$mem"
  407. AppVar calcvar $membuff
  408. update calc.gc 1 $calcvar
  409.  
  410. xButton 146 134 33 15 M+
  411. setvar membuff "\$$mem"
  412. CLI 'eval $calcvar + $membuff TO env:$mem'
  413. setvar membuff "\$$mem"
  414. update calc.gc 5 $membuff
  415.  
  416. xButton 179 134 33 15 M-
  417. setvar membuff "\$$mem"
  418. CLI 'eval $membuff - $calcvar TO env:$mem'
  419. setvar membuff "\$$mem"
  420. update calc.gc 5 $membuff
  421.  
  422.  
  423.  
  424.  
  425.